from django.db import models
from django.contrib.auth.models import AbstractUser
from apps.common.base.models import BaseModel
from apps.system.role.models import Role
from apps.system.rank.models import Rank
from apps.system.team.models import Team
from apps.system.identity.models import Identity
from apps.common.base.models import CustomManager

class UserModel(AbstractUser, BaseModel):
    """扩展Django自带的User模型"""
    gender = (
        ("M", "男"),
        ("W", "女"),
    )
    checkStatus = (
        ("PASS", "通过"),
        ("REFUSE", "拒绝"),
        ("CHECKING", "审核中"),
    )

    objects = CustomManager()

    role=models.ForeignKey(Role, related_name='role_user', to_field='key', on_delete=models.SET_NULL, null=True, blank=True, db_constraint=False)
    # vip=models.ForeignKey(Rank, related_name='vip_user', verbose_name='vip级别', on_delete=models.SET_NULL, null=True, blank=True, db_constraint=False)
    rank=models.ForeignKey(Rank, related_name='rank_user', verbose_name='级别', on_delete=models.SET_NULL, null=True, blank=True, db_constraint=False)
    team=models.ForeignKey(Team, related_name='team_user', verbose_name='团队级别', on_delete=models.SET_NULL, null=True, blank=True, db_constraint=False)
    identity=models.ForeignKey(Identity, related_name='identity_user', verbose_name='股东身份', on_delete=models.SET_NULL, null=True, blank=True, db_constraint=False)
    parent=models.ForeignKey('UserModel', related_name='parent_user', on_delete=models.SET_NULL, null=True, blank=True, db_constraint=False)
    # tags = models.ManyToManyField(Tag, related_name='tags_user', blank=True, default=[], db_constraint=False)

    code = models.CharField("编码", max_length=200, blank=True, null=True, default='')
    phone = models.CharField("手机号", max_length=200, blank=True, null=True, default='')
    nickname = models.CharField("昵称", max_length=200, blank=True, null=True, default='')
    realname = models.CharField("姓名", max_length=200, blank=True, null=True, default='')

    idCard = models.CharField("身份证号码", max_length=200, blank=True, default='')
    avatarUrl = models.CharField("头像", max_length=200, blank=True)
    gender = models.CharField("性别", max_length=200, choices=gender, blank=True, default='')
    age = models.CharField("年龄", max_length=200, blank=True, default='')
    height = models.CharField("身高", max_length=200, blank=True, default='')
    weight = models.CharField("体重", max_length=200, blank=True, default='')
    weixin = models.CharField("微信", max_length=200, blank=True, default='')
    birthday = models.DateTimeField('出生日期', blank=True, null=True)
    email = models.CharField("电子邮箱", max_length=200, blank=True, default='')
    city = models.CharField("现居地", max_length=200, blank=True, default='')
    job = models.CharField("职业", max_length=200, blank=True, default='')
    checkStatus = models.CharField("审核状态", max_length=100, choices=checkStatus, blank=True, default='CHECKING')
    status = models.IntegerField("状态", default=1)
    lastVisitTime = models.DateTimeField('最近访问日期', auto_now=True)
    upRankTime = models.DateTimeField('升级时间', blank=True, null=True)
    isRobot = models.BooleanField('是否是机器人', default=0)

    intro_zwms = models.CharField("自我描述", max_length=510, blank=True, default='')

    historyIncomes = models.DecimalField("历史累计积分", max_digits=20, decimal_places=2, default=0.00)
    incomes = models.DecimalField("消费积分", max_digits=20, decimal_places=2, default=0.00)
    incomesLocked = models.DecimalField("锁定积分", max_digits=20, decimal_places=2, default=0.00)
    balance = models.DecimalField("当前余额", max_digits=20, decimal_places=2, default=0.00)
    balanceLocked = models.DecimalField("锁定余额", max_digits=20, decimal_places=2, default=0.00)
    incomesCredit = models.DecimalField("抵扣积分", max_digits=20, decimal_places=2, default=0.00)
    incomesOriginal = models.DecimalField("原积分", max_digits=20, decimal_places=2, default=0.00)
    contributionValue = models.IntegerField("贡献值", default=0)
    cashSellingMoney = models.DecimalField("结余销售额", max_digits=20, decimal_places=2, default=0.00)
    shop_balance = models.DecimalField("商户余额", max_digits=20, decimal_places=2, default=0.00)

    selfPerformance = models.DecimalField("自己的业绩", max_digits=20, decimal_places=2, default=0.00)
    verticalPeopleNumber = models.IntegerField("直推人数", default=0)
    verticalOrderNumber = models.IntegerField("直推单数", default=0)
    verticalPerformance = models.DecimalField("直推业绩", max_digits=20, decimal_places=2, default=0.00)
    teamPeopleNumber = models.IntegerField("团队人数", default=0)
    teamOrderNumber = models.IntegerField("团队单数", default=0)
    teamPerformance = models.DecimalField("团队业绩", max_digits=20, decimal_places=2, default=0.00)
    
    class Meta:
        verbose_name = '用户模型'
        verbose_name_plural = verbose_name
